package org.creavi.engine.model;

import java.io.IOException;

import com.jme.scene.TriMesh;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;

public class MeshPart implements Savable {
	private float keyframe;

	private TriMesh mesh;

	private int uses;
	
	public MeshPart() {
		
	}

	public MeshPart(float keyframe, TriMesh mesh) {
		super();
		this.keyframe = keyframe;
		this.mesh = mesh;
		uses = 0;
	}

	public float getKeyframe() {
		return keyframe;
	}

	public void setKeyframe(float keyframe) {
		this.keyframe = keyframe;
	}

	public TriMesh getMesh() {
		return mesh;
	}

	public void setMesh(TriMesh mesh) {
		this.mesh = mesh;
	}

	public int getUses() {
		return uses;
	}

	public void setUses(int uses) {
		this.uses = uses;
	}
	
	public synchronized void use() {
		uses++;
	}

	public synchronized void release() {
		uses--;
	}

	public Class<?> getClassTag() {
		return getClass();
	}

	public void read(JMEImporter im) throws IOException {
		InputCapsule ic = im.getCapsule(this);
		keyframe = ic.readFloat("keyframe", 0);
		mesh = (TriMesh)ic.readSavable("mesh", null);
		uses = ic.readInt("uses", 0);
	}

	public void write(JMEExporter ex) throws IOException {
		OutputCapsule oc = ex.getCapsule(this);
		oc.write(keyframe, "keyframe", 0);
		oc.write(mesh, "mesh", null);
		oc.write(0, "uses", 0);
	}
}
